﻿using Apps.Common;
using Apps.Models;
using System.Linq;
using System.Collections.Generic;
using System;
using Apps.Models.PUB;

namespace Apps.BLL.PUB
{
    public partial class PUB_DEVICEBLL
    {

        public override List<PUB_DEVICEModel> GetListByParentId(ref GridPager pager, string queryStr, object parentId)
        {
            string pid = parentId.ToString();

            var whereLambda = PredicateBuilder.True<PUB_DEVICE>();
            if (!string.IsNullOrWhiteSpace(pid) && pid != "0")
            {
                whereLambda = whereLambda.And(p => p.NODE_ID == pid);
            }
            if (!string.IsNullOrWhiteSpace(queryStr))
            {
                whereLambda = whereLambda.And(a => ( a.DEVICE_NAME.Contains(queryStr)
                                   || a.IP.Contains(queryStr)
                                   || a.PARAMETERS.Contains(queryStr)
                                 ));
            }
            IQueryable<PUB_DEVICE> queryData = m_Rep.GetList().Where(whereLambda.Compile()).AsQueryable();
            pager.totalRows = queryData.Count();
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }
        public override List<PUB_DEVICEModel> CreateModelList(ref IQueryable<PUB_DEVICE> queryData)
        {
            List<PUB_DEVICEModel> modelList = (from r in queryData
                                               select new PUB_DEVICEModel
                                               {
                                                   DEVICE_ID = r.DEVICE_ID,
                                                   NODE_ID = r.NODE_ID,
                                                   DEVICE_NAME = r.DEVICE_NAME,
                                                   MODEL_ID = r.MODEL_ID,
                                                   IP = r.IP,
                                                   PORT = r.PORT,
                                                   PARAMETERS = r.PARAMETERS,
                                                   NET_STATUS = r.NET_STATUS,
                                                   NET_STATUS_TIME = r.NET_STATUS_TIME,
                                                   OF_NODEName = r.PUB_OF_NODE.NODE_NAME,
                                                   MODELNAME = r.PUB_DEVICEMODEL == null ? "" : r.PUB_DEVICEMODEL.MODEL_NAME

                                               }).ToList();
            return modelList;
        }
        public override PUB_DEVICEModel GetById(object id)
        {
            if (IsExists(id))
            {

                PUB_DEVICE entity = m_Rep.GetById(id);
                PUB_DEVICEModel model = new PUB_DEVICEModel();
                model.DEVICE_ID = entity.DEVICE_ID;
                model.NODE_ID = entity.NODE_ID;
                model.DEVICE_NAME = entity.DEVICE_NAME;
                model.MODEL_ID = entity.MODEL_ID;
                model.IP = entity.IP;
                model.PORT = entity.PORT;
                model.PARAMETERS = entity.PARAMETERS;
                model.NET_STATUS = entity.NET_STATUS;
                model.NET_STATUS_TIME = entity.NET_STATUS_TIME;
                model.OF_NODEName = entity.PUB_OF_NODE.NODE_NAME;
                model.MODELNAME = entity.PUB_DEVICEMODEL.MODEL_NAME;
                return model;
            }
            else
            {
                return null;
            }
        }
    }
}

